package com.zhuoyue.biz.core.command.cmd.common.faa;

import com.zhuoyue.biz.core.api.domain.DeviceAndGatewayDO;
import com.zhuoyue.biz.core.api.dto.req.ControlReq;

import com.zhuoyue.biz.core.command.cmd.common.fe6.HongWaiXueXiYi0Fe60202;
import com.zhuoyue.biz.core.command.model.BaseParams;
import com.zhuoyue.common.constant.ControlType;
import lombok.Data;
import lombok.Getter;

import java.util.Map;

/**
 * 光速语音配置
 * Sol->speed of light
 *
 * @author wjming
 */
public class SolVoice0Faa8a11 {
    /**
     * 设置型号
     */
    private static final String AIR_SET_TYPE = "0201${gatewayMac}${deviceMac}03EF0F02${modelNumber}";

    /**
     * 空调控制
     */
    private static final String AIR_CONTROL = "0201${gatewayMac}${deviceMac}0FAAFF0840000000${status}${mode}${wind}${temperature}";

    /**
     * 场景配置
     */
    private static final String VOICE_SET_SCENE = "0201${gatewayMac}${deviceMac}1B59${eventCode}12${sceneCode}0000000000000000000000000000000000";


    /**
     * 语音场景指令
     */
    private static final String VOICE_SCENE_COMMAND = "0201${gatewayMac}${deviceMac}1B5A${eventCode}02${voiceSceneCode}";
    /**
     * 虚拟设备配置(除空调外)
     */
    private static final String VIRTUAL_DEVICE = "0201${gatewayMac}${deviceMac}1B5B${code}08${endDeviceMac}";

    /**
     * 空调语音的绑定
     */
    private static final String VIRTUAL_AC_DEVICE = "0201${gatewayMac}${deviceMac}1B5B${code}0A${endDeviceMac}${address}FF";

    /**
     * 重置命令
     */
    private static final String REST = "0201${gatewayMac}${deviceMac}03F301020000";
    /**
     * 设备重启命令
     */
    private static final String RESTART = "2201${gatewayMac}${deviceMac}";


    public static String control(DeviceAndGatewayDO deviceAndGatewayDO, ControlReq controlReq) {
        String cmd = "";
        String gatewayMac = deviceAndGatewayDO.getGatewayMac();
        String deviceMac = deviceAndGatewayDO.getZigbeeMac();
        Map<String, Object> actions = controlReq.getActions();
        switch (controlReq.getControlType()) {
            //空调对码
            case ControlType.AC_SET_MODEL_NUMBER:
                cmd = new SolVoiceModelNumber(gatewayMac, deviceMac, (String) actions.get("modelNumber")).getCmd(AIR_SET_TYPE);
                break;
            //开控制
            case ControlType.TURN_ON_REQUEST:
                cmd = new SolVoiceModelNumber(gatewayMac, deviceMac, "01", "FF", "FF", "FF").getCmd(AIR_CONTROL);
                break;
            case ControlType.TURN_OFF_REQUEST:
                cmd = new SolVoiceModelNumber(gatewayMac, deviceMac, "02", "FF", "FF", "FF").getCmd(AIR_CONTROL);
                break;
            case ControlType.SET_MODE_REQUEST:
                String mode = AcEnum.getAcEnum((String) actions.get("mode"));
                cmd = new SolVoiceModelNumber(gatewayMac, deviceMac, "FF", mode, "FF", "FF").getCmd(AIR_CONTROL);
                break;
            case ControlType.SET_FAN_SPEED_REQUEST:
                String wind = AcEnum.getAcEnum((String) actions.get("wind"));
                cmd = new SolVoiceModelNumber(gatewayMac, deviceMac, "FF", "FF", wind, "FF").getCmd(AIR_CONTROL);
                break;
            case ControlType.SET_TEMPERATURE_REQUEST:
                String temperature = (String) actions.get("temperature");
                cmd = new SolVoiceModelNumber(gatewayMac, deviceMac, "FF", "FF", "FF", temperature).getCmd(AIR_CONTROL);
                break;
            //配置场景
            case ControlType.SET_SCENE:
                String event = (String) actions.get("eventCode");
                String code = (String) actions.get("sceneCode");
                cmd = new SolVoiceModelNumber(gatewayMac, deviceMac, event, code).getCmd(VOICE_SET_SCENE);
                break;
            //配置场景语音
            case ControlType.VOICE_COMMAND:
                String eventCode = EventCodeEnum.getEventCodeEnum((String) actions.get("eventCode"));
                String voiceSceneCode = (String) actions.get("voiceSceneCode");
                cmd = new VoiceSceneCommand(gatewayMac, deviceMac, eventCode, voiceSceneCode).getCmd(VOICE_SCENE_COMMAND);
                break;
            //虚拟窗帘
            case ControlType.Sol.SET_VIRTUAL_CURTAIN_GROUP:
            case ControlType.Sol.SET_VIRTUAL_CURTAIN:
                String endDeviceMac = (String) actions.get("endDeviceMac");
                cmd = new SolVirtualSet(gatewayMac, deviceMac, "02", endDeviceMac).getCmd(VIRTUAL_DEVICE);
                break;
            //设置调光灯
            case ControlType.Sol.SET_VIRTUAL_DIMMING_LAMP_GROUP:
            case ControlType.Sol.SET_VIRTUAL_DIMMING_LAMP:
                cmd = new SolVirtualSet(gatewayMac, deviceMac, "03", (String) actions.get("endDeviceMac")).getCmd(VIRTUAL_DEVICE);
                break;
            //设置智能灯
            case ControlType.Sol.SET_VIRTUAL_INTELLIGENT_LAMP_GROUP:
            case ControlType.Sol.SET_VIRTUAL_INTELLIGENT_LAMP:
                cmd = new SolVirtualSet(gatewayMac, deviceMac, "04", (String) actions.get("endDeviceMac")).getCmd(VIRTUAL_DEVICE);
                break;
            //虚拟空调配置
            case ControlType.Sol.SET_VIRTUAL_AC_PANEL:
                cmd = new SolVirtualSet(gatewayMac, deviceMac, "05", (String) actions.get("endDeviceMac"), (String) actions.get("address")).getCmd(VIRTUAL_AC_DEVICE);
                break;
            //虚拟空调配置
            case ControlType.Sol.SET_VIRTUAL_AC_CONTROLLER:
                cmd = new SolVirtualSet(gatewayMac, deviceMac, "06", (String) actions.get("endDeviceMac"), (String) actions.get("address")).getCmd(VIRTUAL_AC_DEVICE);
                break;
            case ControlType.Sol.REST:
                String cmd1 = new BaseParams(gatewayMac, deviceMac).getCmd(REST);
                String cmd2 = new BaseParams(gatewayMac, deviceMac).getCmd(RESTART);
                cmd = cmd1 + cmd2;
                break;
            case ControlType.SET_TIMED_TASK:
            case ControlType.SET_TIMED_TASK_STATUS:
                //删除定时任务
            case ControlType.DELETE_TIMED_TASK:
                cmd = HongWaiXueXiYi0Fe60202.control(deviceAndGatewayDO, controlReq);
                break;
            default:
                cmd = "";
        }
        return cmd;
    }
}

/**
 * 光速语音数据正配置参数
 */
@Data
class SolVoiceModelNumber extends BaseParams {
    /**
     * 对码
     */
    private String modelNumber;
    /**
     * 空调状态
     */
    private String status;
    /**
     * 空调模式
     * 03=制冷，04=制热，05=除湿，06=送风
     */
    private String mode;
    /**
     * 风速
     */
    private String wind;
    /**
     * 温度
     */
    private String temperature;

    /**
     * 事件
     */
    private String eventCode;
    /**
     * 场景编号
     */
    private String sceneCode;
    /**
     * 结束设备MAC
     */
    private String endDeviceMac;

    /**
     * 空调对码
     *
     * @param gatewayMac
     * @param deviceMac
     * @param modelNumber
     */
    public SolVoiceModelNumber(String gatewayMac, String deviceMac, String modelNumber) {
        super(gatewayMac, deviceMac);
        this.modelNumber = modelNumber;
    }

    /**
     * 空调控制
     *
     * @param gatewayMac
     * @param deviceMac
     * @param status
     * @param mode
     * @param wind
     * @param temperature
     */
    public SolVoiceModelNumber(String gatewayMac, String deviceMac, String status, String mode, String wind, String temperature) {
        super(gatewayMac, deviceMac);
        this.status = status;
        this.mode = mode;
        this.wind = wind;
        this.temperature = temperature;
    }

    /**
     * 场景绑定
     *
     * @param gatewayMac
     * @param deviceMac
     * @param eventCode
     * @param sceneCode
     */
    public SolVoiceModelNumber(String gatewayMac, String deviceMac, String eventCode, String sceneCode) {
        super(gatewayMac, deviceMac);
        this.eventCode = eventCode;
        this.sceneCode = sceneCode;
    }


}

/**
 * 虚拟设备配置
 */
@Data
class SolVirtualSet extends BaseParams {
    /**
     * 空调编号、虚拟设备编号
     */
    private String code;
    /**
     * 结束设备MAC
     */
    private String endDeviceMac;
    /**
     * 地址码
     */
    private String address;

    /**
     * 虚拟设备配置
     *
     * @param gatewayMac
     * @param deviceMac
     * @param code
     * @param endDeviceMac
     */
    public SolVirtualSet(String gatewayMac, String deviceMac, String code, String endDeviceMac) {
        super(gatewayMac, deviceMac);
        this.code = code;
        this.endDeviceMac = endDeviceMac;
    }

    public SolVirtualSet(String gatewayMac, String deviceMac, String code, String endDeviceMac, String address) {
        super(gatewayMac, deviceMac);
        this.code = code;
        this.endDeviceMac = endDeviceMac;
        this.address = address;
    }
}

@Data
class VoiceSceneCommand extends BaseParams {
    private String eventCode;
    private String voiceSceneCode;

    public VoiceSceneCommand(String gatewayMac, String deviceMac, String eventCode, String voiceSceneCode) {
        super(gatewayMac, deviceMac);
        this.eventCode = eventCode;
        this.voiceSceneCode = voiceSceneCode;
    }
}

@Getter
enum AcEnum {
    /**
     * 制冷模式
     */
    COLD_MODEL("COLD", "03"),

    /**
     * 制热模式
     */
    HEAT_MODEL("HEAT", "04"),

    /**
     * 除湿模式
     */
    DEHUMIDIFICATION_MODEL("DEHUMIDIFICATION", "05"),

    /**
     * 送风模式
     */
    WIND_MODEL("WIND", "06"),

    /**
     * 低风
     */
    LOW_WIND("LOW", "02"),

    /**
     * 中风
     */
    MID_WIND("MID", "03"),

    /**
     * 高风
     */
    HIGH_WIND("HIGH", "04"),

    /**
     * 自动
     */
    AUTO_WIND("AUTO", "05");


    private String key;
    private String value;

    AcEnum(String key, String value) {
        this.key = key;
        this.value = value;
    }

    /**
     * 根据Key得到枚举的Value
     * 普通for循环遍历，比较判断
     *
     * @param key
     * @return
     */
    public static String getAcEnum(String key) {
        AcEnum[] acWindEnums = AcEnum.values();
        for (int i = 0; i < acWindEnums.length; i++) {
            if (key.equals(acWindEnums[i].getKey())) {
                return acWindEnums[i].getValue();
            }
        }
        return "";
    }
}

@Getter
enum EventCodeEnum {

    /**
     * 场景1
     */
    scene01("01", "06"),

    /**
     * 场景2
     */
    scene02("02", "07"),

    /**
     * 场景3
     */
    scene03("03", "08"),

    /**
     * 场景4
     */
    scene04("04", "09"),

    /**
     * 场景5
     */
    scene05("05", "0A"),

    /**
     * 场景6
     */
    scene06("06", "0B"),

    /**
     * 场景7
     */
    scene07("07", "0C"),

    /**
     * 场景8
     */
    scene08("08", "0D");


    private String key;
    private String value;

    EventCodeEnum(String key, String value) {
        this.key = key;
        this.value = value;
    }

    /**
     * 根据Key得到枚举的Value
     * 普通for循环遍历，比较判断
     *
     * @param key
     * @return
     */
    public static String getEventCodeEnum(String key) {
        EventCodeEnum[] eventCodeEnums = EventCodeEnum.values();
        for (int i = 0; i < eventCodeEnums.length; i++) {
            if (key.equals(eventCodeEnums[i].getKey())) {
                return eventCodeEnums[i].getValue();
            }
        }
        return "";
    }

}
